<?php
/*
Template Name: Events Handler
Auther: Naseer Ahnad
Created: 27-03-2012
*/
?>
<?php

function get_post_meta_all($post_id){
    global $wpdb;
    $data   =   array();
    $wpdb->query("
        SELECT `meta_key`, `meta_value`
        FROM $wpdb->postmeta
        WHERE `post_id` = $post_id
    ");
    foreach($wpdb->last_result as $k => $v){
        $data[$v->meta_key] =   $v->meta_value;
    };
    return $data;
}

//////// Load Info if Called For Edit //
if(isset($_GET['id'])){		/////////// Edit
	$eventInfo= get_post($_GET['id'], ARRAY_A); 
	$meta = get_post_meta_all($_GET['id']);
	$eventInfo['eventLocation']=$meta['eventLocation'];
	$eventInfo['eventDate']=$meta['eventDate'];
	$eventInfo['userID']=$meta['userID'];
	$act="Update";
}
else{				///////// New Event
	wp_get_current_user();
	$userID = $current_user->ID;
	$eventInfo=array('eventDate'	=> $_GET['date']);
	$eventInfo['userID']=$userID;
	$act="Save";
}

////////////// Action againest act
if(isset($_GET['act'])){
//	print "<pre>";
//	print_r($_POST);
//	print_r($_GET);
//	print "</pre>";
//	exit;
	
	if($_GET['act']=='Save'){
	// Create post object
	  $my_post = array(
		 'post_title' => $_POST['title'],
		 'post_content' => $_POST['description'],
		 'post_status' => 'publish',
		 'post_author' => 1,
		 'post_category' => array(19)
	  );
	
	// Insert the post into the database
	 $id =  wp_insert_post( $my_post );		
	 add_post_meta($id, "eventLocation", $_POST['location']);
	 add_post_meta($id, "eventDate", date("Y-m-d", strtotime($_POST['EventDate'])));
	 add_post_meta($id, "userID", $_POST['MemberID']);
	}
	else if($_GET['act']=='Update'){
		  $my_post = array();
		  $my_post['ID'] = $_POST['EventID'];
		  $my_post['post_title'] = $_POST['title'];
		  $my_post['post_content'] = $_POST['description'];
		// Update the post into the database
		  wp_update_post( $my_post );		
		  update_post_meta($id, "eventLocation", $_POST['location']);
		  update_post_meta($id, "eventDate", date("Y-m-d", strtotime($_POST['EventDate'])));
		  update_post_meta($id, "userID", $_POST['MemberID']);
	} else if($_GET['act']=='Del'){
		$rs= wp_delete_post($_GET['EventID']);
	}
	
	
	echo '<script type="text/javascript">';
	echo 'window.close()';
	echo '</script>';
	exit;
	
}



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Event Information</title>

<script type="text/javascript">
function delInfo(){
	if(confirm("are you sure? do you want to delete it?")){
		eID="<?php echo $eventInfo['ID'] ?>";
		window.location="http://armchairweddingguru.co.uk/event-handler/?act=Del&EventID="+eID;
	}
}
</script>

</head>

<body>


<form method="post" action="http://armchairweddingguru.co.uk/event-handler/?act=<?=$act?>">
<input type="hidden" name="EventID" value="<?=$eventInfo['ID']?>" />
<input type="hidden" name="MemberID" value="<?=$eventInfo['userID']?>" />
<table>

<tr>
	<td>Event Date</td>
    <td><input type="text" name="EventDate" size="39" readonly="readonly" value="<?= date("F j, Y",strtotime($eventInfo['eventDate']))?>"  /></td>
</tr>

<tr>
	<td>Event Title</td>
    <td><input type="text" name="title" size="39" value="<?=$eventInfo['post_title']?>"  /></td>
</tr>

<tr>
	<td>Event Location</td>
    <td><input type="text" name="location" size="39" value="<?=$eventInfo['eventLocation']?>" /></td>
</tr>

<tr>
	<td>Event Description</td>
    <td><textarea name="description" rows="2" cols="30"><?=$eventInfo['post_content']?></textarea></td>
</tr>

<tr>
	<td colspan="2" align="center">
    	<input type="submit" name="submit" value="<?=$act?> Information" />
        <?php if($act=="Update"):?>
        <input type="button" value="Delete Information" onclick="delInfo()" />
        <?php endif; ?>
    </td>
</tr>


</table>

</form>

</body>
</html>